package com.tao.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.SelectOne;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tao.pojo.Answerer;
import com.tao.pojo.Submittable;
import com.tao.service.SubmittableService;
import com.tao.mapper.SubmittableMapper;
import com.tao.service.impl.Mq.SubmitSendMqService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 *
 */
@Service
@CacheConfig(cacheNames = "SubmitTable")
public class SubmittableServiceImpl extends ServiceImpl<SubmittableMapper, Submittable>
    implements SubmittableService{

    @Autowired
    SubmitSendMqService submitSendMqService;


    @Override
    @CacheEvict(key = "'entity.all'")
    public boolean save(Submittable entity) {
        return super.save(entity);
    }

    @Override
    @Cacheable(key = "'entity.all'")
    public List<Submittable> list() {
        return super.list();
    }

    @Override
    @Cacheable(key = "'entity.'+#p0")
    public Submittable getById(Serializable id) {
        return super.getById(id);
    }

    @Override
    @Caching(evict = {@CacheEvict(key = "'entity.'+#p0"),@CacheEvict(key = "'entity.all'")})
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    @Override
    @Caching(evict = {@CacheEvict(key = "'entity.'+#p0"),@CacheEvict(key = "'entity.all'")})
    public boolean remove(Wrapper<Submittable> queryWrapper) {
        return super.remove(queryWrapper);
    }


    @Override
    @Caching(evict = {
            @CacheEvict(key = "'entity.'+#p0.id"),
            @CacheEvict(value = "SubmitTable::selectByAid::",key = "#p0.aid"),
            @CacheEvict(value = "SubmitTable::selectByQid::",key = "#p0.qid")
    })
    public boolean updateById(Submittable entity) {
        return super.updateById(entity);
    }



    @Override
    public boolean sendSubmitTableToMq(Submittable submittable) {
//        存数据库
        this.save(submittable);
//        发送消息到mq 等待处理
        submitSendMqService.sendSubmitTable(submittable.getId());
        return true;
    }




    @Override
    @Caching(evict = {
            @CacheEvict(value = "SubmitTable::selectByAid",allEntries = true),
            @CacheEvict(value = "SubmitTable::selectByQid",allEntries = true)
    })
    public boolean receiveMsg(Long id) {
        try {
            Submittable byId = this.getById(id);

//            如果已经过期 就反返回了
            if (byId.getStatus()==2){
                return  false;
            }

            byId.setStatus(1);
            this.updateById(byId);
            return true;
        } catch (Exception e) {
            return  false;
        }
    }


    @Override
    public boolean refuseSt(Long id) {
        try {
            Submittable byId = this.getById(id);
//            订单状态设置为2 ，手动过期
            byId.setStatus(2);
            this.updateById(byId);
            return true;
        } catch (Exception e) {
            return false;
        }
    }


    @Override
    @Cacheable(key = "'selectByAid::'+#p0")
    public List<Submittable> selectByAid(Long aid) {
        QueryWrapper<Submittable> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("aid",aid);
        queryWrapper.orderBy(true,true,"aid");
        return this.list(queryWrapper);
    }

    @Override
    @Cacheable(key = "'selectByQid::'+#p0")
    public List<Submittable> selectByQid(Long qid) {
        QueryWrapper<Submittable> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("qid",qid);
        queryWrapper.orderBy(true,true,"qid");
        return this.list(queryWrapper);
    }




    /*
     *
     *       questioner 操作
     *
     */
}




